package com.eshop.eshop2021_v1.serviceImpl;

import com.eshop.eshop2021_v1.common.JsonResult;
import com.eshop.eshop2021_v1.mapper.before.CartMapper;
import com.eshop.eshop2021_v1.mapper.before.GoodsMapper;
import com.eshop.eshop2021_v1.pojo.Cart;
import com.eshop.eshop2021_v1.pojo.Goods;
import com.eshop.eshop2021_v1.pojo.User;
import com.eshop.eshop2021_v1.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author: 若若叶
 * @date: 2021/10/13 14:09 FileName: CartServiceImpl Description:
 */
@Service
public class CartServiceImpl implements CartService {

  @Autowired CartMapper cartMapper;
  @Autowired GoodsMapper goodsMapper;

  @Override
  public JsonResult addCart(Integer goodId, Integer goodNum, HttpSession session) {
    if (null == goodId) {
      return new JsonResult(201, "商品下架", null);
    }
    if (null == goodNum || goodNum < 1) {
      return new JsonResult(202, "至少1个", null);
    }
    User loginUser = (User) session.getAttribute("loginUser");
    if (null == loginUser) {
      return new JsonResult(203, "请重新登录", null);
    }

    Cart cartData = cartMapper.findCartInfoByUidAndDoodId(loginUser.getUid(), goodId);

    if (null != cartData) {
      // 根据商品id获取商品单价
      Goods goods = goodsMapper.findGoodByGoodId(goodId);
      if (null == goods) {
        return new JsonResult(206, "商品获取失败", null);
      }
      // 获取单价
      Double grprice = goods.getGrprice();
      // 获取购买数量
      Integer num = cartData.getNum();
      goodNum += num;
      // 得到总价
      double price = grprice * goodNum;
      BigDecimal totalPrice = new BigDecimal(Double.toString(price));
      Cart cart = new Cart();
      cart.setUserId(loginUser.getUid());
      cart.setGoodId(goodId);
      cart.setTotalPrice(totalPrice);
      cart.setNum(goodNum);
      cart.setCartId(cartData.getCartId());
      int row = cartMapper.updateCartInfoByCartId(cart);
      if (row != 1) {
        return new JsonResult(205, "添加失败", null);
      }
      return new JsonResult(200, "添加成功", num);
    }
    // 根据商品id获取商品单价
    Goods goods = goodsMapper.findGoodByGoodId(goodId);
    if (null == goods) {
      return new JsonResult(206, "商品获取失败", null);
    }
    // 获取单价
    Double grprice = goods.getGrprice();

    Cart cart = new Cart();
    // 价格
    // 转成BigDecimal
    BigDecimal totalPrice = new BigDecimal(Double.toString(grprice));
    cart.setTotalPrice(totalPrice);
    cart.setNum(goodNum);
    cart.setGoodId(goodId);
    cart.setUserId(loginUser.getUid());
    int row = cartMapper.addCart(cart);
    if (row != 1) {
      return new JsonResult(204, "添加失败", null);
    }
    return new JsonResult(200, "成功", null);
  }

  @Override
  public List<Cart> findCartAndGoodsInfo(HttpSession session) {
    // 调用持久层userId
    User user = (User) session.getAttribute("loginUser");
    List<Cart> list = cartMapper.findCartGoodsByUserId(user.getUid());
    return list;
  }

  @Override
  public JsonResult deleteOne(Integer cartId) {
    int row = cartMapper.deleteOne(cartId);
    if (row == 1) {
      return new JsonResult(200, "删除成功", null);
    }
    return new JsonResult(201, "删除失败，请重试", null);
  }

  @Override
  public JsonResult deleteAll(HttpSession session) {
    User loginUser = (User) session.getAttribute("loginUser");
    int row = cartMapper.deleteAll(loginUser.getUid());
    if (row != 0) {
      return new JsonResult(200, "清空成功", null);
    }
    return new JsonResult(201, "您还没有添加商品啊亲", null);
  }
}
